﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp.text;
using iTextSharp.text.pdf;
using YunSpace.Core.Pdf;
using YunSpace.Core.Pdf.Params;
using YunSpace.Domain.ViewModel.SiteMgr.Pdf;

namespace YunSpace.Application.Infrastructure
{
    /// <summary>
    /// PDF YunSpace 模板
    /// </summary>
    public class PdfYSTemplate
    {
        #region private fields

        private readonly PdfProvider _pdfProvider;
        private readonly string _imgPrefix;

        #endregion private fields

        #region ctor

        public PdfYSTemplate(string toSaveFileName = null, string imgPrefix = "")
        {
            _pdfProvider = new PdfProvider(toSaveFileName ?? $"Pdf/{DateTime.Now:yyyyMMdd_hhmmss}.pdf", 500, 300);
            _imgPrefix = imgPrefix;
        }

        #endregion ctor

        #region 创建第一页

        /// <summary>
        /// 创建第一页模板（已废弃）
        /// </summary>
        public void CreatePageFor1()
        {
            var pdfProvider = CreateTemplate("上海场地方案");

            //右边栏
            _pdfProvider.Draw(_imgPrefix + "1/rightBar.png", 400, 25, 100, 275);

            var titles = new string[] { "船台", "船厂", "西岸艺术中心", "中成智谷秀场" };
            var locationDescriptions = new string[] { "上海市黄浦区南京西路399号（近黄陂北路口）", "上海市普陀区大渡河路158号（近光复西路）", "上海市长宁区长宁路1018号（近汇川路）", "上海市浦东新区新金桥路1599号" };
            var siteTypes = new string[] { "秀场展馆", "秀场展馆", "艺术画廊", "艺术画廊" };

            var table = pdfProvider.CreateTable(60);

            for (var i = 0; i < 4; i++)
            {
                table.AddCell(_pdfProvider.CreateImgCell(_imgPrefix + "1/t1.png", 10));
                table.AddCell(_pdfProvider.CreateEmptyCell());

                var innerTable = new PdfPTable(1);
                innerTable.AddCell(_pdfProvider.CreateCell(titles[i], 1, 1, 5, Element.ALIGN_LEFT, 10, 0));
                innerTable.AddCell(_pdfProvider.CreateBlankSpaceCell(1, 1, 3));
                innerTable.AddCell(_pdfProvider.CreateCell(locationDescriptions[i], 1, 1, 5, Element.ALIGN_LEFT, 7, 0, new RgbParam(0, 255, 0)));

                innerTable.AddCell(_pdfProvider.CreateCell(siteTypes[i], 1, 1, 5, Element.ALIGN_LEFT, 7, 0, new RgbParam(255, 0, 0)));
                table.AddCell(new PdfPCell(innerTable) { Colspan = 30, Border = 0 });

                table.AddCell(_pdfProvider.CreateEmptyCell(19));

                if (i != 3)
                {
                    table.AddCell(_pdfProvider.CreateBlankSpaceCell(60, 1, 2));
                }
            }

            pdfProvider.AddToDocument(table);

            for (var i = 1; i <= 4; i++)
            {
                //按顺序覆盖
                _pdfProvider.DrawContent(i.ToString(), 10, 255, 0, 0, 26, 255 - (56 * (i - 1)));
            }
        }

        #endregion 创建第一页

        #region 创建第二页

        /// <summary>
        /// 创建第二页模板
        /// </summary>
        public void CreatePageFor2(CreateSitePdfDto dto, List<string> spaceImgs, List<SpaceMarketAttrPdfDto> spaceMarkAttrPdfDtos = null)
        {
            spaceMarkAttrPdfDtos = spaceMarkAttrPdfDtos ?? new List<SpaceMarketAttrPdfDto>();

            var pdfProvider = CreateTemplate(dto.SiteName);

            //创建三列两行表格，固定宽度，6个单元格
            var table = pdfProvider.CreateTable(62);
            const int defaultColspan = 20;

            //第一行图片
            table.AddCell(pdfProvider.CreateImgCell(spaceImgs[0], defaultColspan, 1, 1, 100));
            table.AddCell(pdfProvider.CreateEmptyCell());

            table.AddCell(pdfProvider.CreateImgCell(spaceImgs[1], defaultColspan, 1, 1, 100));
            table.AddCell(pdfProvider.CreateEmptyCell());

            table.AddCell(pdfProvider.CreateImgCell(spaceImgs[2], defaultColspan, 1, 1, 100));

            //第二行文字
            //列要填充完全（如设置 50 列就需要将 50 列填充完整），不然不显示
            const int defaultHorizontalAlignment = Element.ALIGN_LEFT;
            const int defaultVerticalAlignment = Element.ALIGN_TOP;
            const int defaultFontSize = 6;

            var leftWordsCell = pdfProvider.CreateCell($@"场地介绍：{(dto.BrandStory.Length <= 300 ? dto.BrandStory.Replace("\n\n", "") : dto.BrandStory.Substring(0, 300).Replace("\n\n", ""))}", defaultColspan, 1, defaultVerticalAlignment, defaultHorizontalAlignment, defaultFontSize, 0);
            leftWordsCell.SetLeading(3, 1);
            leftWordsCell.Padding = -1;

            table.AddCell(leftWordsCell);
            table.AddCell(pdfProvider.CreateEmptyCell());

            var middleWordsCell = pdfProvider.CreateCell(
                $@"场地风格：{dto.SiteStyle}{Chunk.NEWLINE}
推荐理由：{dto.RecommendReason}{Chunk.NEWLINE}
商场定位：{string.Join("，", spaceMarkAttrPdfDtos.Where(x => !x.StoreType.IsNullOrEmpty()).Select(x => x.StoreType))}{Chunk.NEWLINE}
客群定位：{string.Join("，", spaceMarkAttrPdfDtos.Where(x => !x.StoreType.IsNullOrEmpty()).Select(x => x.GuestType))}{Chunk.NEWLINE}
商场内品牌：{string.Join("，", spaceMarkAttrPdfDtos.Where(x => !x.StoreType.IsNullOrEmpty()).Select(x => x.SignBrand))}{Chunk.NEWLINE}
            ", defaultColspan, 1, defaultVerticalAlignment, defaultHorizontalAlignment, defaultFontSize, 0);

            table.AddCell(middleWordsCell);
            table.AddCell(pdfProvider.CreateEmptyCell());

            var rightWordsCell = pdfProvider.CreateCell(
                $@"城市：{dto.City}{Chunk.NEWLINE}
行政区：{dto.District}{Chunk.NEWLINE}
地址：{dto.Address}{Chunk.NEWLINE}
商圈：{dto.BusinessArea}{Chunk.NEWLINE}
距地铁里程：{dto.SubwayDistance}{Chunk.NEWLINE}
离核心市区距离（KM）:{dto.LoopDistance}{Chunk.NEWLINE}
周边写字楼租金（元/天/平）：{dto.RentOfficeBuilding}{Chunk.NEWLINE}
周边住宅售价(元/平) ：{dto.RentResidentialBuilding}{Chunk.NEWLINE}
            ", defaultColspan, 1, defaultVerticalAlignment, defaultHorizontalAlignment, defaultFontSize, 0);

            table.AddCell(rightWordsCell);

            //保存
            pdfProvider.AddToDocument(table);
            //pdfProvider.Save();
        }

        #endregion 创建第二页

        #region 创建第三页

        /// <summary>
        /// 创建第三页模板
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="imgFileName"></param>
        /// <param name="dto"></param>
        /// <param name="facilities">配套设施</param>
        public void CreatePageFor3(string siteName, string imgFileName, SpacePdfDto dto, string facilities)
        {
            var pdfProvider = CreateTemplate(siteName + "-" + dto.SpaceName);

            var table = pdfProvider.CreateTable(2);

            PdfPCell cell = null;

            table.AddCell(pdfProvider.CreateImgCell(imgFileName));

            cell = new PdfPCell(GenerateTable(dto, facilities)) { Rowspan = 2, Border = 0 };
            table.AddCell(cell);

            //需要把 另一个 cell 填充才能展示表格数据
            //图片需要设置始终复制
            var descriptionCell = CreatePdfCell("空间名称：" + dto.SpaceName);
            descriptionCell.Border = 0;
            descriptionCell.Colspan = 1;
            table.AddCell(descriptionCell);
            //table.AddCell(descriptionCell);
            pdfProvider.AddToDocument(table);
        }

        private static PdfPTable GenerateTable(SpacePdfDto dto, string facilities)
        {
            var bfChinese = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            var fontChinese = new iTextSharp.text.Font(bfChinese, 8, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0));

            var cell = new PdfPCell(new Phrase("基础信息", fontChinese)) { Colspan = 18, BackgroundColor = new BaseColor(208, 208, 208) };
            var table = new PdfPTable(19);

            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.HorizontalAlignment = Element.ALIGN_CENTER;

            var emptyCell = CreatePdfCell("");
            emptyCell.Rowspan = 20;
            emptyCell.Colspan = 1;
            emptyCell.Border = 0;
            table.AddCell(emptyCell);

            table.AddCell(cell);

            table.AddCell(CreatePdfCell("可使用面积（平米）"));
            table.AddCell(CreatePdfCell(dto.Area == null ? " " : dto.Area.ToString() + "  "));

            table.AddCell(CreatePdfCell("长*宽(米）"));
            table.AddCell(CreatePdfCell(dto.LengthWidth + "  "));

            table.AddCell(CreatePdfCell("所处楼层"));
            table.AddCell(CreatePdfCell(dto.CurrentFloor + "  "));

            table.AddCell(CreatePdfCell("空间层高"));
            table.AddCell(CreatePdfCell(dto.Height == null ? " " : dto.Height.ToString() + "  "));

            table.AddCell(CreatePdfCell("可搭建层高"));
            table.AddCell(CreatePdfCell(dto.LimitHeight?.ToString(CultureInfo.InvariantCulture) + "  " ?? " "));

            table.AddCell(CreatePdfCell("可容纳人数（人）"));
            table.AddCell(CreatePdfCell(dto.MaxSeatingCapacity == null ? "  " : dto.MaxSeatingCapacity.ToString() + "  "));

            table.AddCell(CreatePdfCell("可否进车"));
            table.AddCell(CreatePdfCell(dto.Vehicle + "  "));

            table.AddCell(CreatePdfCell("平日人流量"));
            table.AddCell(CreatePdfCell(dto.Visitors + "  "));

            table.AddCell(CreatePdfCell("周末人流量"));
            table.AddCell(CreatePdfCell(dto.WeekendFlow + "  "));

            table.AddCell(CreatePdfCell("配套设施"));
            table.AddCell(CreatePdfCell(facilities + "  "));

            var price1Cell = CreatePdfCell("周一~周四价格");
            price1Cell.Rowspan = 2;

            table.AddCell(price1Cell);
            table.AddCell(CreatePdfCell("活动  " + (dto.OrdinaryMarketPrice == null ? "" : FormatTenThousand(dto.OrdinaryMarketPrice.Value)) + "  万元/天"));

            string priceMarketBuild;

            if (dto.PriceMarketBuild.IsNullOrEmpty())
            {
                priceMarketBuild = "";
            }
            else
            {
                var price = decimal.TryParse(dto.PriceMarketBuild, out var temp);
                priceMarketBuild = FormatTenThousand(temp);
            }


            table.AddCell(CreatePdfCell("搭建  " + priceMarketBuild + "  万元/天"));

            var price2Cell = CreatePdfCell("周五~周日价格");
            price2Cell.Rowspan = 2;

            table.AddCell(price2Cell);
            table.AddCell(CreatePdfCell("活动   " + (dto.MarketPrice == null ? "" : FormatTenThousand(dto.MarketPrice.Value)) + "   万元/天"));

            var weekPriceCell = CreatePdfCell("搭建   " + priceMarketBuild + "   万元/天");
            table.AddCell(weekPriceCell);

            var emptyCell2 = CreatePdfCell(" ");
            emptyCell2.Colspan = 19;
            emptyCell2.Border = 0;
            table.AddCell(emptyCell2);

            return table;
        }

        public static PdfPCell CreatePdfCell(string content, int fontSize = 7)
        {
            var bfChinese = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            var fontChinese = new iTextSharp.text.Font(bfChinese, fontSize, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0));

            var cell = new PdfPCell(new Phrase(content, fontChinese))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER,
                Colspan = 9,

            };

            return cell;
        }

        #endregion 创建第三页

        #region 创建第四页


        /// <summary>
        /// 创建第四页模板
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="imgFileName"></param>
        /// <param name="spaceName"></param>
        /// <param name="planeImg">平面图</param>
        public void CreatePageFor4(string siteName, string imgFileName, string spaceName, string planeImg = null)
        {
            var pdfProvider = CreateTemplate(siteName + "-" + spaceName);

            var table = pdfProvider.CreateTable(2);

            var leftCell = new PdfPCell(Image.GetInstance(imgFileName), true)
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                Border = 0
            };
            table.AddCell(leftCell);

            var innerTable = new PdfPTable(19);

            //设定列后，其它 cell 也需要设置占用列
            innerTable.AddCell(pdfProvider.CreateEmptyCell(1, 20));

            if (!planeImg.IsNullOrEmpty())
            {
                innerTable.AddCell(pdfProvider.CreateImgCell(planeImg, 18));
            }

            //将一个 table 包裹在 cell 中，设置边框为 0，可以隐藏边框
            table.AddCell(new PdfPCell(innerTable) { Border = 0 });

            var descriptionCell = pdfProvider.CreateCell("空间名称：" + spaceName);
            descriptionCell.Border = 0;
            descriptionCell.Colspan = 1;
            //descriptionCell.PaddingTop = 40;

            table.AddCell(descriptionCell);
            table.AddCell(pdfProvider.CreateEmptyCell());

            pdfProvider.AddToDocument(table);
        }

        #endregion 创建第四页

        #region 创建第五页

        /// <summary>
        /// 创建第五页模板
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="dto"></param>
        /// <param name="activitiyCaseForPageImgs"></param>
        public void CreatePageFor5(string siteName, ActivitiesCasePdfDto dto, List<string> activitiyCaseForPageImgs)
        {
            const int defaultColspan = 20;

            var pdfProvider = CreateTemplate($"{siteName}-" + dto.ActivitiesTitle);

            const int numColumns = 62;
            var table = pdfProvider.CreateTable(numColumns);

            table.AddCell(pdfProvider.CreateBlankSpaceCell(numColumns));

            table.AddCell(pdfProvider.CreateCell($@"活动名称：{dto.ActivitiesTitle}", 62, 1, Element.ALIGN_LEFT, Element.ALIGN_LEFT, 8, 0));

            if (!dto.StarDate.IsNullOrEmpty() || !dto.EndDate.IsNullOrEmpty())
            {
                table.AddCell(pdfProvider.CreateCell($@"活动时间：{dto.StarDate} {(dto.EndDate.IsNullOrEmpty() ? "" : "至")} {dto.EndDate}", 62, 1, Element.ALIGN_LEFT, Element.ALIGN_LEFT, 8, 0));
            }


            table.AddCell(pdfProvider.CreateBlankSpaceCell(numColumns));
            table.AddCell(pdfProvider.CreateBlankSpaceCell(numColumns));

            foreach (var activitiyCaseForPageImg in activitiyCaseForPageImgs)
            {
                table.AddCell(pdfProvider.CreateImgCell(activitiyCaseForPageImg, defaultColspan));
                table.AddCell(pdfProvider.CreateEmptyCell());
            }

            pdfProvider.AddToDocument(table);
        }

        #endregion 创建第五页

        #region 创建第六页

        /// <summary>
        /// 创建第六页模板
        /// </summary>
        public void CreatePageFor6()
        {
            const int defaultColspan = 20;

            var pdfProvider = CreateTemplate("TMG艺术中心-空间活动案例2");

            const int numColumns = 62;
            var table = pdfProvider.CreateTable(numColumns);

            table.AddCell(pdfProvider.CreateBlankSpaceCell(numColumns));

            table.AddCell(pdfProvider.CreateCell($@"活动名称：宝马极致体验全国巡演", 62, 1, Element.ALIGN_LEFT, Element.ALIGN_LEFT, 8, 0));

            table.AddCell(pdfProvider.CreateCell($@"活动时间：2017年7月", 62, 1, Element.ALIGN_LEFT, Element.ALIGN_LEFT, 8, 0));

            table.AddCell(pdfProvider.CreateBlankSpaceCell(numColumns));
            table.AddCell(pdfProvider.CreateBlankSpaceCell(numColumns));

            table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "5/t1.png", defaultColspan));
            table.AddCell(pdfProvider.CreateEmptyCell());
            table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "5/t2.png", defaultColspan));
            table.AddCell(pdfProvider.CreateEmptyCell());
            table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "5/t3.png", defaultColspan));

            pdfProvider.AddToDocument(table);

        }

        #endregion 创建第六页

        #region 创建第七页

        /// <summary>
        /// 创建第七页模板
        /// </summary>
        /// <param name="siteName"></param>
        /// <param name="imgFileNames"></param>
        public void CreatePageFor7(string siteName, List<KeyValuePair<string, string>> imgFileNames)
        {
            const int defaultColspan = 20;

            var pdfProvider = CreateTemplate(siteName + "-相关图片");

            const int numColumns = 62;
            var table = pdfProvider.CreateTable(numColumns);

            if (imgFileNames.Count == 1)
            {
                table.AddCell(pdfProvider.CreateImgCell(imgFileNames[0].Key, defaultColspan, 1, 1, 90));
                table.AddCell(pdfProvider.CreateEmptyCell(numColumns - defaultColspan));

                table.AddCell(pdfProvider.CreateCell(imgFileNames[0].Value, defaultColspan, 1, 5, 1, 6, 0));
                table.AddCell(pdfProvider.CreateEmptyCell(numColumns - defaultColspan));
            }
            else if (imgFileNames.Count == 2)
            {
                table.AddCell(pdfProvider.CreateImgCell(imgFileNames[0].Key, defaultColspan, 1, 1, 90));
                table.AddCell(pdfProvider.CreateEmptyCell());
                table.AddCell(pdfProvider.CreateImgCell(imgFileNames[1].Key, defaultColspan, 1, 1, 90));
                table.AddCell(pdfProvider.CreateEmptyCell(numColumns - defaultColspan * 2 - 1));

                table.AddCell(pdfProvider.CreateCell(imgFileNames[0].Value, defaultColspan, 1, 5, 1, 6, 0));
                table.AddCell(pdfProvider.CreateEmptyCell());
                table.AddCell(pdfProvider.CreateCell(imgFileNames[1].Value, defaultColspan, 1, 5, 1, 6, 0));
                table.AddCell(pdfProvider.CreateEmptyCell(numColumns - defaultColspan * 2 - 1));
            }
            else if (imgFileNames.Count == 3)
            {
                var index = 0;
                foreach (var t in imgFileNames)
                {
                    table.AddCell(pdfProvider.CreateImgCell(t.Key, defaultColspan, 1, 1, 90));
                    index++;

                    if (index != 3)
                    {
                        table.AddCell(pdfProvider.CreateEmptyCell());
                    }
                }

                index = 0;

                foreach (var t in imgFileNames)
                {
                    table.AddCell(pdfProvider.CreateCell(t.Value, defaultColspan, 1, 5, 1, 6, 0));
                    index++;

                    if (index != 3)
                    {
                        table.AddCell(pdfProvider.CreateEmptyCell());
                    }
                }

            }
            else if (imgFileNames.Count == 4)
            {
                for (var i = 0; i < 3; i++)
                {
                    table.AddCell(pdfProvider.CreateImgCell(imgFileNames[i].Key, defaultColspan, 1, 1, 90));

                    if ((i + 1) % 3 != 0)
                    {
                        table.AddCell(pdfProvider.CreateEmptyCell());
                    }
                }

                for (var i = 0; i < 3; i++)
                {
                    table.AddCell(pdfProvider.CreateCell(imgFileNames[i].Value, defaultColspan, 1, 5, 1, 6, 0));

                    if ((i + 1) % 3 != 0)
                    {
                        table.AddCell(pdfProvider.CreateEmptyCell());
                    }
                }

                table.AddCell(pdfProvider.CreateImgCell(imgFileNames[3].Key, defaultColspan, 1, 1, 90));
                table.AddCell(pdfProvider.CreateEmptyCell(numColumns - defaultColspan));

                table.AddCell(pdfProvider.CreateCell(imgFileNames[3].Value));
                table.AddCell(pdfProvider.CreateEmptyCell(numColumns - defaultColspan));
            }
            else if (imgFileNames.Count == 5)
            {
                for (var i = 0; i < 3; i++)
                {
                    table.AddCell(pdfProvider.CreateImgCell(imgFileNames[i].Key, defaultColspan, 1, 1, 90));

                    if ((i + 1) % 3 != 0)
                    {
                        table.AddCell(pdfProvider.CreateEmptyCell());
                    }
                }

                for (var i = 0; i < 3; i++)
                {
                    table.AddCell(pdfProvider.CreateCell(imgFileNames[i].Value, defaultColspan, 1, 5, 1, 6, 0));

                    if ((i + 1) % 3 != 0)
                    {
                        table.AddCell(pdfProvider.CreateEmptyCell());
                    }
                }

                table.AddCell(pdfProvider.CreateImgCell(imgFileNames[3].Key, defaultColspan, 1, 1, 90));
                table.AddCell(pdfProvider.CreateEmptyCell());

                table.AddCell(pdfProvider.CreateImgCell(imgFileNames[4].Key, defaultColspan, 1, 1, 90));
                table.AddCell(pdfProvider.CreateEmptyCell(numColumns - defaultColspan * 2 - 1));

                table.AddCell(pdfProvider.CreateCell(imgFileNames[3].Value, defaultColspan, 1, 5, 1, 6, 0));
                table.AddCell(pdfProvider.CreateEmptyCell());

                table.AddCell(pdfProvider.CreateCell(imgFileNames[4].Value, defaultColspan, 1, 5, 1, 6, 0));
                table.AddCell(pdfProvider.CreateEmptyCell(numColumns - defaultColspan * 2 - 1));
            }
            else
            {
                for (var i = 0; i < 3; i++)
                {
                    table.AddCell(pdfProvider.CreateImgCell(imgFileNames[i].Key, defaultColspan, 1, 1, 90));

                    if ((i + 1) % 3 != 0)
                    {
                        table.AddCell(pdfProvider.CreateEmptyCell());
                    }
                }

                for (var i = 0; i < 3; i++)
                {
                    table.AddCell(pdfProvider.CreateCell(imgFileNames[i].Value, defaultColspan, 1, 5, 1, 6, 0));

                    if ((i + 1) % 3 != 0)
                    {
                        table.AddCell(pdfProvider.CreateEmptyCell());
                    }
                }

                for (var i = 3; i < 6; i++)
                {
                    table.AddCell(pdfProvider.CreateImgCell(imgFileNames[i].Key, defaultColspan, 1, 1, 90));

                    if ((i + 1) % 3 != 0)
                    {
                        table.AddCell(pdfProvider.CreateEmptyCell());
                    }
                }

                for (var i = 3; i < 6; i++)
                {
                    table.AddCell(pdfProvider.CreateCell(imgFileNames[i].Value, defaultColspan, 1, 5, 1, 6, 0));

                    if ((i + 1) % 3 != 0)
                    {
                        table.AddCell(pdfProvider.CreateEmptyCell());
                    }
                }
            }


            //for (var i = 0; i < imgFileNamesd.Count; i++)
            //{
            //    table.AddCell(pdfProvider.CreateImgCell(imgFileNamesd[i], defaultColspan));
            //    table.AddCell(pdfProvider.CreateEmptyCell());
            //}

            //table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "7/t1.png", defaultColspan));
            //table.AddCell(pdfProvider.CreateEmptyCell());
            //table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "7/t2.png", defaultColspan));
            //table.AddCell(pdfProvider.CreateEmptyCell());
            //table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "7/t3.png", defaultColspan));

            ////第 2 行
            //table.AddCell(pdfProvider.CreateCell("秀场序厅", defaultColspan, 1, 5, 1, 6, 0));
            //table.AddCell(pdfProvider.CreateEmptyCell());
            //table.AddCell(pdfProvider.CreateCell("休息室", defaultColspan, 1, 5, 1, 6, 0));
            //table.AddCell(pdfProvider.CreateEmptyCell());
            //table.AddCell(pdfProvider.CreateCell("室外周边", defaultColspan, 1, 5, 1, 6, 0));

            ////空行
            //// table.AddCell(pdfProvider.CreateBlankSpaceCell(numColumns));

            ////第 3 行
            //table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "7/t1.png", defaultColspan));
            //table.AddCell(pdfProvider.CreateEmptyCell());
            //table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "7/t2.png", defaultColspan));
            //table.AddCell(pdfProvider.CreateEmptyCell());
            //table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "7/t3.png", defaultColspan));

            ////第 4 行
            //table.AddCell(pdfProvider.CreateCell("入口处", defaultColspan, 1, 5, 1, 6, 0));
            //table.AddCell(pdfProvider.CreateEmptyCell());
            //table.AddCell(pdfProvider.CreateCell("VIP室", defaultColspan, 1, 5, 1, 6, 0));
            //table.AddCell(pdfProvider.CreateEmptyCell());
            //table.AddCell(pdfProvider.CreateCell("空间外景", defaultColspan, 1, 5, 1, 6, 0));

            //table.AddCell(pdfProvider.CreateBlankSpaceCell(numColumns));

            //table.AddCell(pdfProvider.CreateCell($@"活动名称：宝马极致体验全国巡演", 62, 1, Element.ALIGN_LEFT, Element.ALIGN_LEFT, 8, 0));

            //table.AddCell(pdfProvider.CreateCell($@"活动时间：2017年7月", 62, 1, Element.ALIGN_LEFT, Element.ALIGN_LEFT, 8, 0));

            //table.AddCell(pdfProvider.CreateBlankSpaceCell(numColumns));
            //table.AddCell(pdfProvider.CreateBlankSpaceCell(numColumns));

            //table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "5/t1.png", defaultColspan));
            //table.AddCell(pdfProvider.CreateEmptyCell());
            //table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "5/t2.png", defaultColspan));
            //table.AddCell(pdfProvider.CreateEmptyCell());
            //table.AddCell(pdfProvider.CreateImgCell(_imgPrefix + "5/t3.png", defaultColspan));

            pdfProvider.AddToDocument(table);

        }

        #endregion 创建第七页

        #region 创建第八页

        /// <summary>
        /// 创建第八页模板
        /// </summary>
        public void CreatePageFor8(string siteName, string img, SpaceSupplementPdfDto dto)
        {
            var pdfProvider = CreateTemplate($"{siteName}-" + dto.Title);

            var table = pdfProvider.CreateTable(2);

            table.AddCell(pdfProvider.CreateImgCell(img));

            var cell = new PdfPCell(GenerateTable8(dto)) { Rowspan = 2, Border = 0 };
            table.AddCell(cell);

            //需要把 另一个 cell 填充才能展示表格数据
            //图片需要设置始终复制
            var descriptionCell = CreatePdfCell($"空间名称：{dto.Title}");
            descriptionCell.Border = 0;
            descriptionCell.Colspan = 1;
            table.AddCell(descriptionCell);

            pdfProvider.AddToDocument(table);
        }

        private static PdfPTable GenerateTable8(SpaceSupplementPdfDto dto)
        {
            var bfChinese = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            var fontChinese = new iTextSharp.text.Font(bfChinese, 6, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0));

            var cell = new PdfPCell(new Phrase("补充信息", fontChinese)) { Colspan = 18, BackgroundColor = new BaseColor(208, 208, 208) };
            var table = new PdfPTable(19);
            cell.VerticalAlignment = Element.ALIGN_MIDDLE;
            cell.HorizontalAlignment = Element.ALIGN_CENTER;

            var emptyCell = CreatePdfCell("");
            emptyCell.Rowspan = 20;
            emptyCell.Colspan = 1;
            emptyCell.Border = 0;
            table.AddCell(emptyCell);

            table.AddCell(cell);

            table.AddCell(CreatePdfCell8("空间吊点承重(KG/个)"));
            table.AddCell(CreatePdfCell8(dto.Ceiling));

            table.AddCell(CreatePdfCell8("吊点数 （个）"));
            table.AddCell(CreatePdfCell8(dto.SomeDigits));

            table.AddCell(CreatePdfCell8("最大吊点总承重数（KG）"));
            table.AddCell(CreatePdfCell8(dto.LargestDigits));

            table.AddCell(CreatePdfCell8("固定座位数（个）"));
            table.AddCell(CreatePdfCell8(dto.FixedSeatNumber == null ? "" : dto.FixedSeatNumber.ToString()));

            table.AddCell(CreatePdfCell8("是否可以通宵搭建"));
            table.AddCell(CreatePdfCell8(dto.IsNightPlays));

            table.AddCell(CreatePdfCell8("通宵搭建价格&时间"));
            table.AddCell(CreatePdfCell8(dto.PriceNightMarketBuild));

            table.AddCell(CreatePdfCell8("可进场时间"));
            table.AddCell(CreatePdfCell8(dto.DepositTime));

            table.AddCell(CreatePdfCell8("可搭建时间"));
            table.AddCell(CreatePdfCell8(dto.BuildTime));

            table.AddCell(CreatePdfCell8("能耗说明（电费）"));
            table.AddCell(CreatePdfCell8(dto.PropertyFee));

            table.AddCell(CreatePdfCell8("能耗说明（水费）"));
            table.AddCell(CreatePdfCell8(dto.CostWater));

            table.AddCell(CreatePdfCell8("供电量(千瓦)"));
            table.AddCell(CreatePdfCell8(dto.PowerSupply));

            table.AddCell(CreatePdfCell8("配电箱"));
            table.AddCell(CreatePdfCell8(""));

            table.AddCell(CreatePdfCell8("保洁费用"));
            table.AddCell(CreatePdfCell8(dto.CostCleaning));

            table.AddCell(CreatePdfCell8("安保费用"));
            table.AddCell(CreatePdfCell8(dto.CostSecurity));

            table.AddCell(CreatePdfCell8("报批预估费用"));
            table.AddCell(CreatePdfCell8(dto.CostApprovalForecast));

            table.AddCell(CreatePdfCell8("报批预估时间"));
            table.AddCell(CreatePdfCell8(""));

            table.AddCell(CreatePdfCell8("其他费用"));

            var otherPrice = dto.OtherPrice;
            table.AddCell(CreatePdfCell8(otherPrice.Length > 50 ? otherPrice.Substring(0, 50) + "..." : otherPrice));

            table.AddCell(CreatePdfCell8("活动现场禁忌"));

            var remark = dto.Remark ?? "";
            table.AddCell(CreatePdfCell8(remark.Length > 50 ? remark.Substring(0, 50) + "..." : remark));

            return table;
        }

        public static PdfPCell CreatePdfCell8(string content, int fontSize = 6)
        {
            var bfChinese = BaseFont.CreateFont("C://WINDOWS//Fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            var fontChinese = new iTextSharp.text.Font(bfChinese, fontSize, iTextSharp.text.Font.NORMAL, new iTextSharp.text.BaseColor(0, 0, 0));

            var cell = new PdfPCell(new Phrase(content, fontChinese))
            {
                VerticalAlignment = Element.ALIGN_MIDDLE,
                HorizontalAlignment = Element.ALIGN_CENTER,
                Colspan = 9
            };

            return cell;
        }

        #endregion 创建第八页

        /// <summary>
        /// 保存
        /// </summary>
        public void Save()
        {
            _pdfProvider.Save();
        }

        /// <summary>
        /// 添加新页面
        /// </summary>
        public void AddNewPage()
        {
            _pdfProvider.Document.NewPage();
        }



        #region private  methods


        /// <summary>
        /// 创建模板
        /// </summary>
        /// <param name="header">头部文字</param>
        /// <returns></returns>
        private PdfProvider CreateTemplate(string header)
        {
            _pdfProvider.Draw(_imgPrefix + "footer.png", 0, 0, 500, 25);
            _pdfProvider.Draw(_imgPrefix + "bar.png", 0, 275, 15, 15);
            _pdfProvider.DrawContent(header, 12, 0, 0, 255, 20, 278);

            return _pdfProvider;
        }

        private static string FormatTenThousand(decimal value)
        {
            return (value / 10000).ToString(CultureInfo.InvariantCulture);
        }

        #endregion private  methods
    }
}
